<?php

class Event extends Controller
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::Controller();

		// Check if the logged user is an administrator
		$this->access_library->check_access();
		
		// Load needed models, libraries, helpers and language files
		$this->load->module_model('admin', 'event_model', 'event');
		
		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'event');
	}

	// Public methods
	public function index()
	{
		$lang_working = load_language_working();
		$data['events'] = $this->event->get_events($lang_working['id']);
		$this->_template['page']	= 'event/list';
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function move_menu_item()
	{
		$item = $this->input->post('item');
		$item_id = explode('_', $item);
		$action = $this->input->post('action');
		if ($action == 'up')
		{
			$this->menu->move_item_up($item_id[1]);
		}
		else if ($action == 'down')
		{
			$this->menu->move_item_down($item_id[1]);
		}
	}

	public function create()
	{	
		$lang_working = load_language_working();
		$this->load->helper('fckeditor');
		$this->form_validation->set_rules('title', 'lang:form_title', 'required|max_length[50]|xss_clean');
		//$this->form_validation->set_rules('content', 'lang:form_content', 'required|xss_clean');
		//$this->form_validation->set_rules('excerpt', 'lang:form_excerpt', 'required|max_length[250]|xss_clean');
		$this->form_validation->set_error_delimiters('', '<br />');
			
		if ($this->form_validation->run() == TRUE)
		{
			$this->event->create_event($lang_working['id']);
			$this->session->set_flashdata('message', lang('successfully_created'));

			redirect('admin/event', 'refresh');
		}
			
		$this->_template['page']	= 'event/create';
			
		$this->system_library->load($this->_template['page'], null, TRUE);
	}

	public function edit($id = null)
	{
		//$lang_working = load_language_working();
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
		$this->load->helper('fckeditor');
		$this->form_validation->set_rules('title', 'lang:form_title', 'required|max_length[50]|xss_clean');
		
		$this->form_validation->set_error_delimiters('', '<br />');
			
		$data['event'] = $this->event->get_event($id);
		//var_dump($data['event']);	
		if ($this->form_validation->run() == TRUE)
		{
			
			$this->event->edit_event($id);
			$this->session->set_flashdata('message', lang('successfully_edited'));
			redirect('admin/event', 'refresh');
		}
		
		$this->_template['page']	= 'event/edit';
			
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}

	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
		$this->menu->change_status_menu_item($id,0);
		redirect('admin/menus', 'refresh');
	}
	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
		$this->menu->change_status_menu_item($id,1);
		redirect('admin/menus', 'refresh');
	}
	public function delete($id)
	{
		$this->navigation->delete_navigation_item($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));

		redirect('admin/navigation', 'refresh');
	}
}

/* End of file event.php */